<?php

namespace App\Api;

use PhalApi\Api;
use PhalApi\Exception\BadRequestException;

/**
 * Img上传接口
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class Img extends Api
{

    public function getRules()
    {
        return array(
            'base64' => array(
                'token' => array('name' => 'token', 'require' => true, 'desc' => 'token'),
                'img'   => array('name' => 'img', 'require' => true, 'desc' => 'base64数据')
            ),
            'all'    => array(
                'token' => array('name' => 'token', 'require' => true, 'desc' => 'token'),
                'img'   => array('name' => 'img', 'require' => true, 'desc' => 'base64数据')
            ),
            'img'    => array(
                'token' => array('name' => 'token', 'require' => true, 'desc' => 'token'),
            ),
            'go'     => array(
                'file'  => array(
                    'name'    => 'file',        // 客户端上传的文件字段
                    'type'    => 'file',
                    'require' => true,
                    'max'     => 10 * 1024 * 1024,                  // 最大允许上传2M = 2 * 1024 * 1024,
                    'range'   => array('image/jpeg', 'image/png'),  // 允许的文件格式
                    'ext'     => 'jpeg,jpg,png',                    // 允许的文件扩展名
                    'desc'    => '待上传的图片文件',
                ),
                'phone' => array('name' => 'phone', 'require' => true, 'min' => 1, 'max' => 11, 'desc' => '手机号')
            ),
        );
    }


    /**
     * Img上传接口
     * @desc Img上传接口
     * @return string url 返回img绝对地址
     */
    public function go()
    {
        // 指定允许其他域名访问
        header('Access-Control-Allow-Origin:*');

        $rs = array('code' => 0, 'url' => '');

        $tmpName = $this->file['tmp_name'];

        $name = md5($this->file['name'] . $_SERVER['REQUEST_TIME']);
        $ext = strrchr($this->file['name'], '.');
        $uploadFolder = sprintf('%s/public/uploads/' . date("Y-m-d") . '/', API_ROOT);
        if (!is_dir($uploadFolder)) {
            mkdir($uploadFolder, 0777);
        }

        $imgPath = $uploadFolder . $this->phone . '-' . $name . $ext;

        if (move_uploaded_file($tmpName, $imgPath)) {
            $rs['code'] = 1;
            $rs['url'] = sprintf(
                'http://%s/phalapi/public/uploads/%s/%s-%s%s',
                $_SERVER['SERVER_NAME'], date("Y-m-d"), $this->phone, $name, $ext);
        }

        return $rs;
    }

    /**
     * img 上传接口
     * @desc Img上传接口
     * @return string url 返回img绝对地址
     */
    public function img()
    {
        // 指定允许其他域名访问
        header('Access-Control-Allow-Origin:*');
        $tokeninfo = \PhalApi\DI()->notorm->rok_account
            ->where('token', $this->token)
            ->where('img', 1)
            ->fetchOne();
        if ($tokeninfo) {
            return '需要截图';
        }
        return '有截图';
    }

    /**
     * all 上传接口
     * @desc Img上传接口
     * @return string url 返回img绝对地址
     */
    public function all()
    {
        // 指定允许其他域名访问
        header('Access-Control-Allow-Origin:*');
        $uploadFolder = sprintf('%s/public/games/', API_ROOT);
        if (!is_dir($uploadFolder)) {
            mkdir($uploadFolder, 0777);
        }
        $imgPath = $uploadFolder . $this->token . '.png';
        $bars64img = urldecode($this->img);
        $rs = file_put_contents($imgPath, base64_decode($bars64img));

        \PhalApi\DI()->notorm->rok_account
            ->where('token', $this->token)
            ->update(
                array('img' => 0)
            );
        return $rs;

    }

    /**
     * Img上传接口
     * @desc Img上传接口
     * @return string url 返回img绝对地址
     */
    public function base64()
    {
        // 指定允许其他域名访问
        header('Access-Control-Allow-Origin:*');

        // 创建文件夹
        $uploadFolder = sprintf('%s/public/uploads/', API_ROOT);
        if (!is_dir($uploadFolder)) {
            mkdir($uploadFolder, 0777);
        }
        // 设置文件名
        $imgPath = $uploadFolder . $this->token . '.png';
        $bars64img = urldecode($this->img);
        $rs = file_put_contents($imgPath, base64_decode($bars64img));


        /**
         * 发起http post请求(REST API), 并获取REST请求的结果
         * @param string $url
         * @param string $param
         * @return - http response body if succeeds, else false.
         */
        function request_post($url = '', $param = '')
        {
            if (empty($url) || empty($param)) {
                return false;
            }

            $postUrl = $url;
            $curlPost = $param;
            // 初始化curl
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $postUrl);
            curl_setopt($curl, CURLOPT_HEADER, 0);
            // 要求结果为字符串且输出到屏幕上
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            // post提交方式
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
            // 运行curl
            $data = curl_exec($curl);
            curl_close($curl);

            return $data;
        }

        $token = '24.97fe07becad7f6c7109e856d61bee55e.2592000.1608353331.282335-23008147';
        $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=' . $token;

        $bodys = array(
            'url' => 'http://rok.honghongdesign.cn/public/uploads/' . $this->token . '.png'
        );
//        $res = request_post($url, $bodys);
//        $res = json_decode($res, true);

//        $game = array();
//        $game['qu'] = $res['words_result'][0]['words'];
//        $game['food'] = $res['words_result'][1]['words'];
//        $game['wood'] = $res['words_result'][2]['words'];
//        $game['stone'] = $res['words_result'][3]['words'];
//        $game['gold'] = $res['words_result'][4]['words'];
//
//        \PhalApi\DI()->notorm->rok_account
//            ->where('token', $this->token)
//            ->update($game);

        return $bodys;


    }
}
